/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
 package kono.utils
{
	public class ListNode
	{
		//简单链表， simple list node
		
		private var _data:*;
		private var _link:ListNode;
		
		//@initData:结点初始值， the node' data;
		public function ListNode(initData:*)
		{
			_data = initData;
			_link = null;
		}
		
		//取得当前结点数据, return the node's data
		public function get nodeData():*
		{
			return _data;
		}
		
		//设置当前结点数据， set the node‘s data
		public function set nodeData(newData:*):void
		{
			_data = newData;
		}
		
		//取得当前结点引用， get the next node that the node being referenced
		public function get link():ListNode
		{
			return _link;
		}
		
		//设置当前结点引用， set the next node that the current node being referenced
		public function set link(newLink:ListNode):void
		{
			_link = newLink;
		}
		
		//返回当前数组长度， get the length of the node
		public  function get nodeLength():uint
		{
			var cursor:ListNode;
			var length:uint = 1;
			
			for(cursor = _link; cursor != null; cursor = cursor.link)
			{
				length++;
			}
			
			return length;
		}
		
		//追加结点， append a new node
		public function addNode(data:*):ListNode
		{
			var newNode:ListNode = new ListNode(data);
			_link = newNode;
			return newNode;
		}
		
		//删除尾结点， remove the last node;
		public function removeNode():void
		{
			_link = _link.link;
		}
		
		//检查是否包含target数值， check if the node contains the given item
		public function hasItem(target:*):Boolean
		{
			if(target == _data)
			{
				return true;
			}
			var cursor:ListNode;			
			for(cursor = _link; cursor != null; cursor = cursor.link)
			{
				if(cursor.nodeData == target)
				{
					return true;
				}
			}
			
			return false;
		}
		
		//取得指定位置的node, get the node at the given position
		public function listPosotion(position:uint):ListNode
		{
			var cursor:ListNode = _link;
			var i:uint;
			if(position == 0)
			{
				return _link;
			}
			for(i=0; (i < position) && (cursor != null); i++)
			{
				cursor = cursor.link;
			}
			return cursor;
		}
				
		//描述当前Node， rerurn a string represent the node		
		public function toString():String
		{
			return "[ListNode, data=" + _data + "]"
		}
	}
}